package com.boge.common.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpClientUtil {


    // 服务器ip
    public static final String IP = "http://localhost";

    // 端口
    public static final String PORT = ":6060";

    // GET请求接口不带参数
    public static final String GET_URL_NO_PARAMS = IP + PORT + "/province";

    // GET请求接口不带参数
    public static String doGetNoParams() {

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 创建 GET 请求
        HttpGet httpGet = new HttpGet(GET_URL_NO_PARAMS);
        httpGet.setHeader("Accept-Encoding", "identity");
        //log.info("【发送GET请求】请求地址为：{}", GET_URL_NO_PARAMS);
        CloseableHttpResponse httpResponse = null;
        try {

            httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            //log.info("【发送GET请求】成功，相应状态为：{}", httpResponse.getStatusLine());
            if (HttpStatus.SC_OK == httpResponse.getStatusLine().getStatusCode() && null != httpEntity) {

                String result = EntityUtils.toString(httpEntity);
                //log.info("【发送GET请求】成功，响应内容为：{}", result);
                return result;
            }
        } catch (IOException e) {

            //log.error("【发送GET请求】失败，执行发送请求时，出现IO异常，异常信息为：{}", e);
            return null;
        } finally {

            try {

                close(httpClient, httpResponse);
            } catch (IOException e) {

                //log.error("【发送GET请求】失败，关闭流时，出现IO异常，异常信息为：{}", e);
            }
        }
        return null;
    }

    // GET请求接口不带参数
    public static String doGetParams(String url) {

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 创建 GET 请求
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("Accept-Encoding", "identity");
        //log.info("【发送GET请求】请求地址为：{}", GET_URL_NO_PARAMS);
        CloseableHttpResponse httpResponse = null;
        try {

            httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            //log.info("【发送GET请求】成功，相应状态为：{}", httpResponse.getStatusLine());
            if (HttpStatus.SC_OK == httpResponse.getStatusLine().getStatusCode() && null != httpEntity) {

                String result = EntityUtils.toString(httpEntity);
                //log.info("【发送GET请求】成功，响应内容为：{}", result);
                return result;
            }
        } catch (IOException e) {

            //log.error("【发送GET请求】失败，执行发送请求时，出现IO异常，异常信息为：{}", e);
            return null;
        } finally {

            try {

                close(httpClient, httpResponse);
            } catch (IOException e) {

                //log.error("【发送GET请求】失败，关闭流时，出现IO异常，异常信息为：{}", e);
            }
        }
        return null;
    }

    // 关闭流
    public static void close(CloseableHttpClient httpClient, CloseableHttpResponse httpResponse) throws IOException{

        if (null != httpClient) {

            httpClient.close();
        }
        if (null != httpResponse) {

            httpResponse.close();
        }
    }
}
